﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using rateITcd.Models;

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace rateITcd.Controllers
{
    public class RestaurantController : Controller
    {
        private RateItDB db = new RateItDB();

        // GET: Restaurant/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
           
            var restaurant = db.Restaurants.Find(id);
            //var restaurantDish = db.Restaurants.Find(d => d.D == id);
            if (restaurant == null)
            {
                return HttpNotFound();
            }
            
            ViewBag.Title = restaurant.Name;
            ViewBag.Restaurant = restaurant;
            ViewBag.Dishes = restaurant.Dishes.ToList();

            return View();
        
        }

        // GET: Restaurant/Create 
        // Only admins can create new restaurants
        public ActionResult Create()
        {
            if (User.IsInRole("Admin"))
            {
                return View();
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // POST: Restaurant/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Phone,AdressStreet,AdressNumber,AdressZip,AdressCity")] Restaurant restaurant)
        {
            // Only admins can create new restaurants
            if (User.IsInRole("Admin"))
            {
                if (ModelState.IsValid)
                {
                    db.Restaurants.Add(restaurant);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(restaurant);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // GET: Restaurant/Edit/5
        public ActionResult Edit(int? id)
        {
            // Only admins can edit restaurants
            if (User.IsInRole("Admin"))
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Restaurant restaurant = db.Restaurants.Find(id);
                if (restaurant == null)
                {
                    return HttpNotFound();
                }
                return View(restaurant);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // POST: Restaurant/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Phone,AdressStreet,AdressNumber,AdressZip,AdressCity")] Restaurant restaurant)
        {
            // Only admins can edit restaurants
            if (User.IsInRole("Admin"))
            {
                if (ModelState.IsValid)
                {
                    db.Entry(restaurant).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(restaurant);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }

        }

        // GET: Restaurant/Delete/5
        public ActionResult Delete(int? id)
        {
            // Only admins can delete restaurants
            if (User.IsInRole("Admin"))
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Restaurant restaurant = db.Restaurants.Find(id);
                if (restaurant == null)
                {
                    return HttpNotFound();
                }
                return View(restaurant);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }

        }

        // POST: Restaurant/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            // Only admins can delete restaurants
            if (User.IsInRole("Admin"))
            {
                Restaurant restaurant = db.Restaurants.Find(id);
                db.Restaurants.Remove(restaurant);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return new HttpUnauthorizedResult();
            }

        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
